package com.bifrost.repository;

import com.bifrost.entity.Product;
import com.bifrost.entity.ProductReview;
import com.bifrost.entity.ProductSku;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author rangjf
 * @date 2017/11/1
 */
public interface ProductReviewRepository extends JpaRepository<ProductReview, Long>, JpaSpecificationExecutor {

    /**
     * 统计有图评论数量
     *
     * @param product
     * @param hasMedia 为 true
     * @return List
     */
    Long countByProductAndHasMedia(Product product, Boolean hasMedia);

    /**
     * 获取子评论
     *
     * @param reviewId reviewId
     * @return List
     */
    List<ProductReview> findAllByParentReview(Long reviewId);

    /**
     * 统计sku已审核评论数量
     */
    @Query(value = "SELECT COUNT(*) from product_review where sku_id=?1 and REVIEW_STATUS=TRUE and PARENT_REVIEW IS NULL" ,nativeQuery = true )
    Integer countFinalSkuReview(ProductSku sku);

    /**
     * 统计product已审核评论数量
     */
    @Query(value = "SELECT COUNT(*) from product_review where PRODUCT_ID=?1 and REVIEW_STATUS=TRUE and PARENT_REVIEW IS NULL" ,nativeQuery = true )
    Integer countFinalProductReview(Product product);
}
